// @ts-ignore
import {sm2} from "sm-crypto";
// @ts-ignore
import {generateKeyPairHex} from "sm-crypto/src/sm2/utils";
// @ts-ignore
import http from "@/conf/axios";

/**
 * 生成密钥对
 */
function doGenerate() {
    let keypair = generateKeyPairHex();
    console.log("key: " + keypair);
    let privateKey = keypair.ecprvhex;
    let publicKey = keypair.ecpubhex;
    console.log("privateKey:" + privateKey);
    console.log("publicKey:" + publicKey);
}

export function cmdSM2Encrypt(str: string, publicKey: string) {
    return new Promise((resolve, reject) => {
        let key = publicKey;
        if (publicKey == "" || publicKey == undefined) {
            reject('解析错误')
        }
        // console.log("公钥："+ key);
        // sm2 加密 模式选择已封装为C1C3C2
        resolve(doCrypt(key, str))
    })
}

/**
 * sm2加密方法
 * @param publickey
 * @param value
 */
export function doCrypt(publickey: string, value: string) {
    let encryptData = sm2.doEncrypt(value, publickey, 1);
    return "04" + encryptData;
}

/**
 * sm2解密方法
 * @param publickey
 * @param msg
 */
export function doDecrypt(privateKey: string, value: string) {
    value = value.slice(2);
    var data = sm2.doDecrypt(value, privateKey, 1);
    // console.log("解密后："+ data );
    return data;
}
